package com.tsd.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.base.entity.BaseAssistData;
import com.tsd.base.entity.BaseAssistDataExt;
import com.tsd.base.service.BaseAssistDataService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.entity.SysUser;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：辅助数据 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class BaseAssistDataController extends SystemBaseController {

    @Resource
    private BaseAssistDataService baseAssistDataService;

    /**
     * 根据id获取辅助数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBaseAssistDataById.action", method = RequestMethod.POST)
    public HttpResult<BaseAssistData> getBaseAssistDataById() {
        HttpResult<BaseAssistData> hr = new HttpResult<BaseAssistData>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            BaseAssistData baseAssistData = baseAssistDataService.getBaseAssistDataById(id);
            hr.setData(baseAssistData);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 根据id删除辅助数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBaseAssistData.action", method = RequestMethod.POST)
    public HttpResult<String> deleteBaseAssistData() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            baseAssistDataService.deleteBaseAssistData(sid, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    /**
     * 保存辅助数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveBaseAssistData.action", method = RequestMethod.POST)
    public HttpResult<String> saveBaseAssistData() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            BaseAssistData t = JSON.parseObject(json, BaseAssistData.class);
            baseAssistDataService.saveBaseAssistData(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 分页查询辅助数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBaseAssistDatas.action", method = RequestMethod.POST)
    public HttpResult<List<BaseAssistDataExt>> findBaseAssistDatas() {
        HttpResult<List<BaseAssistDataExt>> hr = new HttpResult<List<BaseAssistDataExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "assist_type_sid", "assist_type_sid", true, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = new PageBean(map);
            List<BaseAssistDataExt> list = baseAssistDataService.findBaseAssistDatas(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 分页查询辅助数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBaseAssistDatasFromTypesNameList.action", method = RequestMethod.POST)
    public HttpResult<Map<String, Object>> findBaseAssistDatasFromTypesNameList() {
        HttpResult<Map<String, Object>> hr = new HttpResult<Map<String, Object>>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            Object type_code_list = map.get("type_code_list");
            super.checkEmpty(type_code_list, "type_code_list");
            Map<String, Object> result = null;
            if (type_code_list instanceof List) {
                List<String> typeCodeList = JSONArray.parseArray(JSON.toJSONString(type_code_list), String.class);
                result = baseAssistDataService.findBaseAssistDataFromTypeNameList(typeCodeList, opUser);
            } else {
                throw new HlpException("type_code_list非数组格式");
            }
            hr.setData(result);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 批量删除辅助数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchDeleteBaseAssistData.action", method = RequestMethod.POST)
    public HttpResult<String> batchDeleteBaseAssistData() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object idObject = map.get("sid_list");
            super.checkEmpty(idObject, "请提供参数sid_list");
            if (idObject instanceof List) {
                List<String> idList = JSONArray.parseArray(JSON.toJSONString(idObject), String.class);
                baseAssistDataService.batchDeleteBaseAssistData(idList, opUser);
            } else {
                throw new HlpException("id_list非数组格式");
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
